import requests
from fake_useragent import *

# requests库的使用

headers = {
    "User-agent": UserAgent().chrome
}


# get请求
def get_dmeo():
    url = "https://www.baidu.com/s?"
    params = {
        "wd": "B站"
    }
    response = requests.get(url, headers=headers, params=params)
    print(response.text)


# post请求
def post_demo():
    login_url = "https://www.w3cschool.cn/checklogin_1"
    data = {
        "username": "username",
        "password": "password"
    }
    response = requests.post(login_url, headers=headers, data=data)
    print(response.text)


# proxy代理
def proxy_demo():
    url = "http://httpbin.org/get"
    proxies = {
        "http": "175.42.122.249:9999",
        # "http": "username:password@ip:port"
    }
    response = requests.get(url, headers=headers, proxies=proxies)
    print(response.text)


# ssl证书和字符编码问题的解决
def ssl_demo():
    url = "https://www.12306.cn/index/"
    # 禁用安全请求警告
    requests.packages.urllib3.disable_warnings()
    response = requests.get(url, headers=headers, verify=False)
    response.encoding = "utf-8"
    print(response.text)


# cookie的使用
def cookie_demo():
    # 通过session进行请求
    session = requests.Session()
    login_url = "https://www.w3cschool.cn/checklogin_1"
    data = {
        "username": "username",
        "password": "password"
    }
    session.post(login_url, headers=headers, data=data)
    info_url = "https://www.w3cschool.cn/my"
    response = session.get(info_url, headers=headers)
    print(response.text)


def mian():
    # get_dmeo()
    # post_demo()
    # proxy_demo()
    # ssl_demo()
    cookie_demo()


if __name__ == '__main__':
    mian()
